package com.cskaoyan.market.controller;

import com.cskaoyan.market.vo.BrandVo;
import com.cskaoyan.market.vo.CategoryVo;
import com.cskaoyan.market.bo.GoodsCombineBo;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.service.AdminGoodsService;
import com.cskaoyan.market.service.impl.AdminGoodsServiceImpl;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/goods/*")
public class AdminGoodsController extends HttpServlet {

    AdminGoodsService adminGoodsService = new AdminGoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/goods/", "");
        if ("list".equals(op)) {
            list(req, resp);
        } else if ("detail".equals(op)) {
            detail(req, resp);
        } else if ("catAndBrand".equals(op)) {
            catAndBrand(req, resp);
        }
    }

    private void catAndBrand(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<CategoryVo> marketCategoryList = adminGoodsService.categoryList();
        List<BrandVo> marketBrandList = adminGoodsService.brandList();
        HashMap<String, List> data = new HashMap<>();
        data.put("brandList", marketBrandList);
        data.put("categoryList", marketCategoryList);
        Object o = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));

    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        MarketGoods marketGoods = adminGoodsService.getGoodsDetail(id);
        List<MarketGoodsAttribute> marketGoodsAttributes = adminGoodsService.getGoodsAttributes(id);
        List<MarketGoodsSpecification> marketGoodsSpecifications = adminGoodsService.getGoodsSpecifications(id);
        List<MarketGoodsProduct> marketGoodsProducts = adminGoodsService.getGoodsProducts(id);

        List<Integer> marketCategoryIds = new ArrayList<>();
        MarketCategory marketCategory = adminGoodsService.findPid(marketGoods.getCategoryId());
        marketCategoryIds.add(marketCategory.getPid());
        marketCategoryIds.add(marketGoods.getCategoryId());

        Map<String, Object> data = new HashMap<>();
        data.put("goods", marketGoods);
        data.put("categoryIds", marketCategoryIds);
        data.put("attributes", marketGoodsAttributes);
        data.put("specifications", marketGoodsSpecifications);
        data.put("products", marketGoodsProducts);
        Object ok = ResponseUtil.ok(data);
        String s = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String goodsId = req.getParameter("goodsId");
        String goodsSn = req.getParameter("goodsSn");
        String name = req.getParameter("name");
        List<MarketGoods> marketGoodsList = adminGoodsService.list(page, limit, sort, order, goodsId, goodsSn, name);
        Object o = ResponseUtil.okList(marketGoodsList);
        String s = JacksonUtil.writeValueAsString(o);
        resp.getWriter().println(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/goods/", "");
        if ("delete".equals(op)) {
            delete(req, resp);
        } else if ("update".equals(op)) {
            update(req, resp);
        }else if ("create".equals(op)) {
            create(req, resp);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        ObjectMapper mapper = JacksonUtil.getObjectMapper();
        // 获取请求体里的json字符串
        GoodsCombineBo combineBo = mapper.readValue(requestBody, GoodsCombineBo.class);
        MarketGoods goods = combineBo.getGoods();
        List<MarketGoodsAttribute> attributes=combineBo.getAttributes();
        List<MarketGoodsSpecification> specifications=combineBo.getSpecifications();
        List<MarketGoodsProduct> products=combineBo.getProducts();
        // todo 做一些校验，比如判空
        adminGoodsService.insert(goods,attributes,specifications,products);
        Object ok = ResponseUtil.ok();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        GoodsCombineBo combineBo = objectMapper.readValue(requestBody, GoodsCombineBo.class);

        MarketGoods goods = combineBo.getGoods();
        List<MarketGoodsAttribute> attributes = combineBo.getAttributes();
        List<MarketGoodsSpecification> specifications = combineBo.getSpecifications();
        List<MarketGoodsProduct> products = combineBo.getProducts();

        adminGoodsService.updateGoods(goods, attributes, products, specifications);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String id = JacksonUtil.parseString(requestBody, "id");
        adminGoodsService.delete(id);
        Object ok = ResponseUtil.ok();
        String s = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(s);
    }
}
